package com.mccree.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * 自定义服务器解码器
 * @author zhabo
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecoder.decode");

        int length = in.readInt();
        byte[] bytes = new byte[length];
        in.readBytes(bytes);


        //将MessageProtocol对象，放入out，传递下一个handler进行处理
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setData(bytes);
        messageProtocol.setLength(length);
        out.add(messageProtocol);
    }
}
